package com.wsd.jing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsd.jing.entity.RolePermission;
import com.wsd.jing.mapper.RolePermissionMapper;
import com.wsd.jing.mapper.UserMapper;
import com.wsd.jing.request.CommonRequest;
import com.wsd.jing.service.IDepartmentService;
import com.wsd.jing.service.IRolePermissionService;
import com.wsd.jing.utils.SortUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @author tom
 * @version 1.0.0
 * @projectName: jing
 * @packageName: com.wsd.jing.service.impl
 * @className: RolePermissionServiceImpl
 * @description: RolePermission服务实现类
 * @createDate 2023-11-10 11:01
 * @copyright 2023-11-10 11:01 www.tom.com Inc. All rights reserved.
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements IRolePermissionService {

    private final RolePermissionMapper rolePermissionMapper;
    private final UserMapper userMapper;
    private final IDepartmentService departmentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertRolePermission(RolePermission rolePermission) {
        return this.save(rolePermission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateRolePermission(RolePermission rolePermission) {
        return this.updateById(rolePermission);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRolePermission(Integer id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteRolePermissions(String ids) {
        List<String> idList = StrUtil.splitTrim(ids, ",");
        return this.removeByIds(idList);
    }

    @Override
    public RolePermission getRolePermission(Integer id) {
        return this.getById(id);
    }

    @Override
    public List<RolePermission> listRolePermissions(CommonRequest commonRequest, RolePermission rolePermission) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件
        if (ObjectUtil.isNotEmpty(rolePermission.getId())) {
            queryWrapper.lambda().eq(RolePermission::getId, rolePermission.getId());
        }
        if (ObjectUtil.isNotEmpty(rolePermission.getRoleId())) {
            queryWrapper.lambda().eq(RolePermission::getRoleId, rolePermission.getRoleId());
        }
        // 处理排序字段
        SortUtil.handleListSort(commonRequest, queryWrapper);
        return this.list(queryWrapper);
    }

    @Override
    public Map<String, Object> listPageRolePermissions(CommonRequest commonRequest, RolePermission rolePermission) {
        Map<String, Object> resultMap = new HashMap<>(2);
        Page<RolePermission> page = new Page<>(commonRequest.getPageNum(), commonRequest.getPageSize());
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        // TODO 设置查询条件

        // 处理排序字段
        SortUtil.handlePageSort(commonRequest, page);
        IPage<RolePermission> result = this.page(page, queryWrapper);
        resultMap.put("rows", result.getRecords());
        resultMap.put("total", result.getTotal());
        return resultMap;
    }
}
